package org.jvnet.hudson.client_demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dpstruc {
	
	public static String JENKINSURL = "http://10.144.97.100:8080/jenkins/job/";
	final static String usr = "arthymoment";
	final static String pwd = "arthymoment";
	
	public static void main(String[] args) throws IOException {
		
		Dbls one = new Dbls("MR_BL_MOrderEJB");
		one.setNextBuildNumber(one.nextBuildNumber());
		one = buildTree(one);
		one.setUp(null);
		
		for (int i=0; i<one.getDown().size(); i++) {
			System.out.println( one.getDown().get(i).getModId() + " "+ one.getDown().get(i).getNextBuildNumber()+one.getDown().get(i).isLeaf());
			if ( one.getDown().get(i).getDown().size()>0 ){
				for (int j=0; j<one.getDown().get(i).getDown().size(); j++) {
					System.out.println( "\t"+one.getDown().get(i).getDown().get(j).getModId() + one.getDown().get(i).getDown().get(j).getNextBuildNumber()+one.getDown().get(i).getDown().get(j).isLeaf() );
				}
			}
		}
		
		System.out.println("MR_BL_DevOps:"+one.isExist(one,"MR_BL_DevOps"));
	}
	
	
	
	private static Dbls buildTree (Dbls nex) throws IOException {
		String urlStr = JENKINSURL+nex.getModId()+"/config.xml";
		
		Dbls selDbls = new Dbls(nex.getModId());
		selDbls.setNextBuildNumber(selDbls.nextBuildNumber());
		selDbls.setUp(nex);
		Map<String,Object> map = new HashMap<String,Object>();
        
        // 連線取得資料
        InputStream res = null; 
        SAXReader reader = new SAXReader();
        try {
        	res = URLConnectionUtil.doGet(urlStr,false,usr,pwd,map);
        	Document document = reader.read(res);
    		Element root = document.getRootElement();
    		Iterator<Element> it = root.elementIterator();	
    		ArrayList<Dbls> dbss = new ArrayList<Dbls>();
        	while (it.hasNext()) {
    			Element e2 = it.next();
    			if ( "publishers".equalsIgnoreCase(e2.getName() ) ) {
    				if( e2.elementIterator().hasNext() ){
    					String tmpStr = e2.element("hudson.tasks.BuildTrigger").element("childProjects").getText();
    					String[] downStreamModId = tmpStr.split(",");	//因為可能下游不只一個
    					
    					for(int i=0;i<downStreamModId.length;i++){
    						Dbls nxs = new Dbls(downStreamModId[i].trim());
    						nxs.setUp(selDbls);
    						dbss.add( buildTree(nxs) );
    					}
    				}
    			}
    		}
        	selDbls.setDown(dbss);
        	
        	selDbls.setLeaf( ( dbss.size()== 0) );
        	
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        	res.close();
        }
		
		return selDbls;
	}
	
	
}

class Dbls {
	private static boolean isNotExist = true;
	public static String JENKINSURL = "http://10.144.97.100:8080/jenkins/job/";
	final static String usr = "arthymoment";
	final static String pwd = "arthymoment";
	public Dbls(String modId) {
		this.modId = modId;
	}
	
	private Dbls up = null;
	private String modId;
	private ArrayList<Dbls> down =null;
	private boolean isLeaf = false;
	private int nextBuildNumber;
	
	public boolean isLeaf() {
		return isLeaf;
	}
	public void setLeaf(boolean isLeaf) {
		this.isLeaf = isLeaf;
	}
	public static boolean isNotExist() {
		return isNotExist;
	}
	public static void setNotExist(boolean isNotExist) {
		Dbls.isNotExist = isNotExist;
	}
	public int getNextBuildNumber() {
		return nextBuildNumber;
	}
	public void setNextBuildNumber(int nextBuildNumber) {
		this.nextBuildNumber = nextBuildNumber;
	}
	public Dbls getUp() {
		return up;
	}
	public void setUp(Dbls up) {
		this.up = up;
	}
	public ArrayList<Dbls> getDown() {
		return down;
	}
	public void setDown(ArrayList<Dbls> down) {
		this.down = down;
	}
	public String getModId() {
		return modId;
	}
	public void setModId(String modId) {
		this.modId = modId;
	}
	public boolean isExist(Dbls obj,String modId) {
		if ( modId.equalsIgnoreCase(this.modId) ) {
			isNotExist = false;
		} else {
			for(int i=0; i<obj.getDown().size();i++){
				isNotExist = !obj.getDown().get(i).isExist(obj.getDown().get(i),modId);
			}
		}
		
		return !isNotExist;
	}
	
	public int nextBuildNumber() throws IOException {
		
		int nextBuildNumber = 0;
		String urlStr = JENKINSURL+this.getModId()+"/api/xml";
		// 連線取得資料
        InputStream res = null; 
        SAXReader reader = new SAXReader();
        Map<String,Object> map = new HashMap<String,Object>();
        try {
        	res = URLConnectionUtil.doGet(urlStr,false,usr,pwd,map);
        	Document document = reader.read(res);
    		Element root = document.getRootElement();
    		Iterator<Element> it = root.elementIterator();	
    		ArrayList<Dbls> dbss = new ArrayList<Dbls>();
        	while (it.hasNext()) {
    			Element e2 = it.next();
    			if ( "nextBuildNumber".equalsIgnoreCase(e2.getName() ) ) {
    				nextBuildNumber = Integer.parseInt(e2.getText());
    			}
    		}
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        	res.close();
        }
        
        return nextBuildNumber;
	}
}

